let mongodb = require("mongodb");

let mongoct = mongodb.MongoClient;

let ObjectId = mongodb.ObjectID;


let sql = "huashuo";

let collection = "pro";
// 连接库
let open = ({
    dbName = sql,
    collectionName = collection,
    url = 'mongodb://127.0.0.1:27017'
}) => {
    return new Promise((resolve, reject) => {
        mongoct.connect(url, {
            useUnifiedTopology: true
        }, (err, client) => {
            if (err) reject(err);
            let db = client.db(dbName);
            let collection = db.collection(collectionName);
            resolve({
                collection,
                client,
                ObjectId
            });
        })
    })
}

// 查询列表

//查询列表 findList
let findList = ({
    collectionName = collection,
    dbName = sql,
    _page,
    _limit,
    _sort,
    q //可选参数
} = {}) => {
    // _page 页数 q 检索 _limit 限定 _sort 排序

    //生成条件
    // let rule = q ? {title: new RegExp('/'+q+'/',g)} : {}
    let rule = q ? {
        title: eval('/' + q + '/')
    } : {}; //目前还不是全文检索，只针对了title

    return new Promise((resolve, reject) => {
        //链库
        open({
            collectionName,
            dbName
        }).then(
            ({
                collection,
                client
            }) => {

                //集合查询
                collection.find(rule, {
                    skip: _page * _limit,
                    limit: _limit,
                    projection: {},
                    sort: {
                        [_sort]: -1
                    }
                }).toArray((err, result) => { //转数组
                    if (!err && result.length > 0) { //集合操作没有失败，数据有长度
                        resolve({
                            err: 0,
                            data: result
                        })
                    } else { //没有查询到
                        resolve({
                            err: 0,
                            msg: '查无数据'
                        })
                    }
                    client.close()
                })
            }
        ).catch(
            err => reject({
                err: 1,
                msg: '库链接失败' + err
            })
        )
    })

}


// 查询详情


let findDetail = ({
    collectionName = collection,
    dbName = sql,
    _id = null
} = {}) => new Promise((resolve, reject) => {
    // 判断id的合法性

    if (_id && _id.length === 24) {
        open({
            collectionName,
            dbName
        }).then(
            ({
                collection,
                client
            }) => {
                //查询
                collection.find({
                    _id: ObjectId(_id)
                }, {
                    projection: {
                        _id: 0
                    }
                }).toArray((err, result) => { //转数组
                    if (!err && result.length > 0) { //集合操作没有失败，数据有长度
                        resolve({
                            err: 0,
                            data: result[0] //通过id查询，回来的result也是数组,给客户端的详情数据是一个对象
                        })
                    } else { //没有查询到
                        resolve({
                            err: 0,
                            msg: '查无数据'
                        })
                    }
                    client.close(); //关库
                })
            }
        ).catch(
            err => reject({
                err: 1,
                msg: '库链接失败' + err
            })
        )
    } else {
        reject({
            err: 1,
            msg: 'id未传递，或长度格式有误'
        })
    }


})



exports.open = open;
exports.findList = findList;
exports.findDetail = findDetail;